//
//  MainTabBar.swift
//  SinaWeiBo20
//
//  Created by tarena on 16/3/18.
//  Copyright © 2016年 tarena. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    //默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        
    }
    
    //如果重写 init(frame:) 系统会默认为这个控件是通过代码创建
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    private func setupUI() {
        //添加按钮
        addSubview(composeBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //手动修改所有 “按钮” 的位置
        //遍历所有的子视图
        let width = self.bounds.size.width / 5
        let height = self.bounds.size.height
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        var index:CGFloat = 0
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subview.frame = CGRectOffset(rect, width * index, 0)
                index += index == 1 ? 2 : 1
            }
        }
        composeBtn.frame = CGRectOffset(rect, width * 2, 0)
    }
    
    
    // 懒加载 
    lazy var composeBtn : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        btn.sizeToFit()
        return btn
    }()
    
}
